﻿using Newtonsoft.Json;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RabbitMQConsumerDemo
{
    class Program
    {
        /// <summary>
        /// 路由名称
        /// </summary>
        const string ExchangeName = "ClassTicket.Exchange";
        //队列名称
        const string QueueName = "ClassTicket.Queue";
        //路由规则
        const string RoutingKey = "#";

        /// 连接配置
        /// </summary>
        private static readonly ConnectionFactory factory = new ConnectionFactory()
        {
            //HostName = "127.0.0.1",
            //UserName = "whl",
            //Password = "123456",
            //Port = 5672,
            //VirtualHost = "/"
            HostName = "10.0.102.73",
            UserName = "wxadmin",
            Password = "wxadmin",
            Port = 5672,
            VirtualHost = "/"
        };

        static void Main(string[] args)
        {
            DirectAcceptExchangeTask();
        }

        public static void DirectAcceptExchangeTask()
        {
            try
            {
                factory.AutomaticRecoveryEnabled = true;
                using (IConnection conn = factory.CreateConnection())
                {
                    using (IModel channel = conn.CreateModel())
                    {
                        //channel.ExchangeDeclare(ExchangeName, ExchangeType.Direct);
                        channel.ExchangeDeclare(ExchangeName, ExchangeType.Direct, true, false, null);
                        //在MQ上定义一个持久化队列，如果名称相同不会重复创建
                        channel.QueueDeclare(QueueName, true, false, false, null);
                        channel.QueueBind(QueueName, ExchangeName, RoutingKey);

                        //这种处理速度较慢，因为循环线程等待。
                        //while (true)
                        //{
                        //    BasicGetResult msgResponse = channel.BasicGet("logs", true);
                        //    if (msgResponse != null)
                        //    {
                        //        var msgBody = Encoding.UTF8.GetString(msgResponse.Body);
                        //        Console.WriteLine(string.Format("***接收时间:{0}，消息内容：{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msgBody));
                        //    }
                        //    System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
                        //}
                        Console.WriteLine("Listening...");
                        var consumer = new EventingBasicConsumer(channel);
                        consumer.Received += (sender, eventArgs) =>
                        {
                            var msgBody = Encoding.UTF8.GetString(eventArgs.Body);
                            if (msgBody != null && msgBody.Length > 0)
                            {
                                Console.WriteLine(string.Format("***接收时间:{0}，消息内容：{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msgBody));
                            }
                        };
                        channel.BasicConsume(QueueName, true, consumer);
                        Console.WriteLine("按任意值，退出程序");
                        Console.ReadKey();
                    }
                }
            }
            catch (Exception e1)
            {
                Console.WriteLine(e1.ToString());
            }
            Console.ReadLine();
        }
    }
}
